<?php
namespace Admin\Controller;
use Think\Controller;

class NewsController extends Controller
{
	//读取新闻内容
	public function index()
	{
		$news = M('news');
		// $res = $news->index();

		$count = $news->count();
		$page = new \Think\Page($count,7);
		$show = $page->show();
		$list = $news->order('addtime desc')->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('show',$show);
		$this->display('index');
	}
	//进入添加新闻页面
	public function addNews_show()
	{
		$this->display('addnews');
	}
	//添加新闻方法
	public function addNews()
	{
		$news = D('news');

		$res = $news->addNews_add();
		if($res=="title"){
			$this->error('标题不能为空','',2);
		}

		if($res=="content"){
			$this->error('内容不能为空','',2);
		}

		if($res=="success"){
			echo "<script>alert('添加成功')</script>";
		}else{
			echo "<script>alert('由于系统原因，添加失败')</script>";
		}
		$this->index();
	}
	//读取要修改的新闻内容并把原数据库的新闻内容展示在修改页面上
	public function edit()
	{
		$news = D('news');
		$res = $news->news_edit();
		$this->assign('list',$res);
		$this->display();
	}
	//修改新闻内容
	public function edit_news()
	{
		$news = D('news');
		$res = $news->edit_Mod();

		if($res=='title'){
			$this->error('标题不能为空','',2);
		}

		if($res=="content"){
			$this->error('内容不能为空','',2);
		}

		if($res=="success"){
			echo "<script>alert('修改成功')</script>";
			$this->index();
		}else{
			$this->display('edit');
		}
	}
	//删除新闻内容
	public function delete()
	{
		$id = I('get.id');
		$map['id'] = ['eq',$id];
		$news = M('news');
		$res = $news->where($map)->delete();

		if($res){
			echo "<script>alert('删除成功')</script>";
			$this->index();
		}else{
			echo "<script>alert('由于系统原因，删除失败')</script>";
		}
	}
}